import WidgetKit
import SwiftUI
import CalendarShared

public struct MediumEntry: TimelineEntry, CalendarEntryable {
    private let pref = Preference.shared
    
    public let date: Date
    public let days: [DayModel]

    public var dayModel: DayModel
    public var configuration: (any Intentable)?
    public var theme: Theme = .system
    public var language: Language = .system
    public var firstWeek: Week = .sun
    public var showFestival: Bool = true
    public var showLunar: Bool = true
    public var showEvent: Bool = false
    public var showReminder: Bool = false
    public var accent: ColorModel = .accent
    public var background: ColorModel = .background
    public var normal: ColorModel = .normal
    public var festival: ColorModel = .festival
    public var work: ColorModel = .work
    public var rest: ColorModel = .rest
    public var week: ColorModel = .week
    public var current: ColorModel = .current
    public var calendar: ColorModel = .calendar
    public var reminder: ColorModel = .reminder
    public var taboo: ColorModel = .taboo
    public var avoid: ColorModel = .avoid

    public init(date: Date, configuration: Intentable? = nil) {
        self.date = date
        self.configuration = configuration
        self.theme = pref.sync ? pref.theme : configuration?.themeMode ?? .system
        self.language = pref.sync ? pref.language : configuration?.languageMode ?? .system
        self.showFestival = pref.sync ? pref.showFestival : configuration?.isShowFestival ?? true
        self.showLunar = pref.sync ? pref.showLunar : configuration?.isShowLunar ?? true
        self.showEvent = pref.sync ? pref.showEvent : configuration?.isShowEvents ?? false
        self.showReminder = pref.sync ? pref.showReminder : configuration?.isShowReminder ?? false
        self.background = pref.sync ? pref.background : configuration?.backgroundColor ?? .background
        self.normal = pref.sync ? pref.normal : configuration?.normalColor ?? .normal
        self.week = pref.sync ? pref.week : configuration?.weekColor ?? .week
        self.festival = pref.sync ? pref.festival : configuration?.festivalColor ?? .festival
        self.current = pref.sync ? pref.current : configuration?.currentColor ?? .current
        self.calendar = pref.sync ? pref.calendar : configuration?.calendarColor ?? .calendar
        self.reminder = pref.sync ? pref.calendar : configuration?.reminderColor ?? .reminder
        self.work = pref.sync ? pref.work : configuration?.workColor ?? .work
        self.rest = pref.sync ? pref.rest : configuration?.restColor ?? .rest
        self.firstWeek = pref.sync ? pref.firstWeek : configuration?.firstWeekMode ?? .sun
        self.days = CalendarShared.shared.getWeek(date, firstWeek: self.firstWeek)
        self.dayModel = CalendarShared.shared.getDay(Date())
    }
}
